<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 * @package    ZNF
 * @subpackage Business
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: DB.php 9 2005-11-23 19:32:20Z freejay $
 * @since      Release 0.7.9
 * @link       http://www.zeronotice.org
 */

require_once('MDB2.php');
require_once('ZNF/Config/DBConfig.php');

/**
 * The <i>ZNF_Business_DB</i> class extends the PEAR <i>DB</i> class.
 *
 * Connects to the database using the configuration taken from the database
 * configuration file. Returns the <i>ZNF_Business_DB</i> object reference with
 * a singleton pattern.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Business
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: DB.php 9 2005-11-23 19:32:20Z freejay $
 * @since      Release 0.8.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Business_MDB2 extends MDB2
{

    /**
     * The database object reference.
     *
     * @access private
     * @staticvar ZNF_Business_DB $_db
     */
    static private $_db;

    /**
     * Prefix used in the name of the database tables.
     *
     * @access private
     * @staticvar string $_tablePrefix
     */
    static private $_tablePrefix;

    /**
     * Private constructor to avoid <i>ZNF_Business_DB</i> object creation.
     *
     * @access private
     */
    private function __construct()
    {
    }

    /**
     * Private clonator to avoid <i>ZNF_Business_DB</i> object clonation.
     *
     * @access private
     */
    private function __clone()
    {
    }

    /**
     * Returns the reference to the <i>ZNF_Business_DB</i> object with a singleton pattern.
     *
     * This object is shared by all the classes that require the access to the
     * database.
     *
     * @static
     * @access public
     * @return ZNF_Business_DB
     */
    static public function getInstance()
    {
        if (!(isset(self::$_db))) {
            $dbConfig = ZNF_Config_DBConfig::getInstance();
            $dsn = "{$dbConfig->getDbms()}://{$dbConfig->getUsername()}:{$dbConfig->getPassword()}@{$dbConfig->getHostname()}/{$dbConfig->getDbname()}";
            self::$_db = self::connect($dsn);

            if (self::isError(self::$_db)) {
                $translation = new ZNF_Presentation_Translation('ZNF', $_SESSION['znf']['lang']);
                throw new ZNF_Business_MDB2Exception($translation->get('errorDbConnection'));
            } else {
                self::$_db->setFetchMode(MDB2_FETCHMODE_ASSOC);
            }
            self::$_tablePrefix = $dbConfig->getTablePrefix();
        }

        return self::$_db;
    }

    /**
     * Returns the prefix used in the name of the database tables.
     *
     * @static
     * @access public
     * @return string
     */
    static public function getTablePrefix()
    {
        return self::$_tablePrefix;
    }

    /**
     * Destroys the <i>ZNF_Business_MDB2</i> object.
     *
     * @access public
     */
    public function __destruct()
    {
    }

}

/**
 * <i>ZNF_Business_MDB2Exception</i> is the exception type for the <i>ZNF_Business_DB</i> class.
 *
 * <i>ZNF_Business_MDB2Exception</i> extends the <i>Exception</i> class of PHP5.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Business
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: DB.php 9 2005-11-23 19:32:20Z freejay $
 * @since      Release 0.8.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Business_MDB2Exception extends Exception
{
}

?>
